package FindGlob;
use strict;

our (@ISA, @EXPORT, @EXPORT_OK);
use Exporter ();
@ISA = qw(Exporter);
@EXPORT = qw(DeleteGlob FindGlob);


# replace all contain GLOB or CODE
# in HASH - remove given key - key is not checked if it is GLOB or CODE
# in ARRAY - set index to undef

sub DeleteGlob {
    my $ref = ref $_[0];
    my $rval = $ref ? $_[0] : \$_[0];
    shift;
    my $seen = shift || {};
    if($ref eq 'GLOB' or $ref eq 'CODE') { return 1 }
    if($ref){
	return if exists $seen->{$rval};
	$seen->{$rval}=1;
    }
    if($ref eq 'HASH') {
	while( my ($k,$v) = each %$rval ) {
	    delete $rval->{$k} if DeleteGlob($v,$seen);
	}
    }
    elsif($ref eq 'ARRAY') {
	foreach my $i (0 .. $#$rval) {
	    $rval->[$i]=undef if DeleteGlob($rval->[$i],$seen);
	}
    }
    return 0;
}

# return true if structure contain GLOB or CODE
sub FindGlob {
    my $ref = ref $_[0];
    my $rval = $ref ? $_[0] : \$_[0];
    shift;
    my $seen = shift || {};
    if($ref){
	return if exists $seen->{$rval};
	$seen->{$rval}=1;
    }
    if($ref eq 'GLOB' or $ref eq 'CODE') { return 1 }
    if($ref eq 'HASH') {
	while( my ($k,$v) = each %$rval ) {
	    return 1 if FindGlob($v,$seen);
	}
    }
    elsif($ref eq 'ARRAY') {
	foreach my $v (@$rval) {
	    return 1 if FindGlob($v,$seen);
	}
    }
    return 0;
}

1;
__END__
# author: Anton Sedlak 2003 <ased@cce.cz>
Package for CGI and storeable when you need remove GLOB and CODE
